package session.servlet;

import session.entity.Cake;
import session.entity.CakeDB;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.List;

@WebServlet(name = "PurchaseServelt", value = "/cake-purchase")
public class PurchaseServelt extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Cake cake = CakeDB.getCakeById(id);
        if(cake == null){
            response.sendRedirect("/cake-list");
            return;
        }
        HttpSession session = request.getSession();
        List<Cake> cart = (List<Cake>) session.getAttribute("cart");
        if(cart == null){
            cart = new java.util.ArrayList<>();
            session.setAttribute("cart",cart);
        }
        cart.add(cake);
        Cookie cookie = new Cookie("JSESSIONID",session.getId());
        cookie.setMaxAge(60*30);
        cookie.setPath("/");
        response.sendRedirect("/cake-cart");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
